package com.gt.quality.apim.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gt.quality.apim.domain.ApiParamInfo;
import com.gt.quality.apim.service.ApiParamInfoService;
import com.gt.quality.base.pojo.BaseLimitRequest;
import com.gt.quality.base.pojo.BaseLimitResponse;
import com.gt.quality.base.pojo.Resp;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

/**
 * API参数管理接口
 *
 * @author makejava
 * @since 2026-11-20 09:51:37
 */
@RestController
@RequestMapping("apiParamInfo")
@Tag(name = "apiParamInfo -RController")
public class ApiParamInfoController  {
    /**
     * 服务对象
     */
    private final ApiParamInfoService apiParamInfoService;

    @Autowired
    public ApiParamInfoController(@Qualifier("apiParamInfoService") ApiParamInfoService apiParamInfoService) {
        this.apiParamInfoService = apiParamInfoService;
    }

   /**
     * 分页查询数据
     *
     * @param limitRequest 查询实体
     * @return 所有数据
     */
    @PostMapping("/queryLimit")
	@Operation(summary = "分页查询请求")
     public Resp<BaseLimitResponse<ApiParamInfo>> queryPage(@RequestBody BaseLimitRequest<ApiParamInfo> limitRequest) {
        // 分页查询
        IPage<ApiParamInfo> page = this.apiParamInfoService.queryLimitPage(limitRequest);
        // 封装返回结果集
        BaseLimitResponse<ApiParamInfo> data = BaseLimitResponse.getInstance(page.getRecords(), page.getTotal(), page.getPages(), limitRequest.getPageIndex(), limitRequest.getPageSize());
        return Resp.ok(data);
    }

    /**
     * 获取全体API参数在某个APi下
     */
    @GetMapping("/listAllByApiId/{id}")
    @Operation(summary = "分页查询请求")
    public Resp<List<ApiParamInfo>> listAllByApiId(@PathVariable("id") Serializable id) {
        QueryWrapper<ApiParamInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("api_info_id",id);
        List<ApiParamInfo> list = this.apiParamInfoService.list(wrapper);
        // 封装返回结果集
        return Resp.ok(list);
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/queryOne/{id}")
	@Operation(summary = "PathVariable路径传参-Id")
	@Parameter(name = "id", description = "id", in = ParameterIn.PATH)
    public Resp<ApiParamInfo> selectOne(@PathVariable("id") Serializable id) {
         return Resp.ok(this.apiParamInfoService.getById(id));
    }

   /**
     * 新增数据
     *
     * @param apiParamInfo 实体对象
     * @return 新增结果
     */
    @PostMapping("/save")
	@Operation(summary = "新增数据")
    public Resp<String> insert(@RequestBody ApiParamInfo apiParamInfo) {
        boolean save = this.apiParamInfoService.save(apiParamInfo);
        String executeMsg = "新增成功,id 是：" + apiParamInfo.getId();
        return save ? Resp.ok(executeMsg) : Resp.error(executeMsg);
    }

     /**
     * 批量新增数据
     *
     * @param apiParamInfoList 实体对象
     * @return 新增结果
     */
    @PostMapping("/saveBatch")
	@Operation(summary = "批量新增数据")
    public Resp<String> insertBatch(@RequestBody List<ApiParamInfo> apiParamInfoList) {
        Integer save = this.apiParamInfoService.saveBatchByEasyBaseMapper(apiParamInfoList);
        String executeMsg = "新增成功=>id 是：" + Arrays.toString(apiParamInfoList.stream().map(ApiParamInfo::getId).toArray());
        return save > 0 ? Resp.ok(executeMsg) : Resp.error(executeMsg);
    }


    /**
     * 修改数据
     *
     * @param apiParamInfo 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
	@Operation(summary = "修改数据")
    public Resp<String> update(@RequestBody ApiParamInfo apiParamInfo) {
        boolean update = this.apiParamInfoService.updateById(apiParamInfo);
        String executeMsg = "修改成功,id 是：" + apiParamInfo.getId();
        return update ? Resp.ok(executeMsg) : Resp.error(executeMsg);
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/dels")
	@Operation(summary = "删除数据-idList 主键结合")
    public Resp<String> delete(@RequestParam("idList") List<Long> idList) {
        boolean delNumber = this.apiParamInfoService.removeByIds(idList);
        String executeMsg = "删除成功,ids 是：" + idList;
        return delNumber ? Resp.ok(executeMsg) : Resp.error(executeMsg);
    }
}
